Component({
  /**
   * 组件的属性列表
   */
  properties: {
    date_show: {
      type: Boolean,
      value: false,
    },
    picker_value: {
      type: String,
      value: "",
    },
    picker_name: {
      type: String,
      value: "",
    },
    label_val: {
      type: String,
      value: "",
    },


  },

  /**
   * 组件的初始数据
   */
  data: {
    weekList: ['一', '二', '三', '四', '五', '六', '日'], //一周
    month: [
      {
        // text   此值为日期值，类型为number
        // now    此值为今天，在本月的周末为false，其它时候为true，类型为布尔
        // gray   此值为日期置灰，本月中的上月和下月日期以及本月的周末，类型为布尔
      }
    ],//一个月所有数据
    nowYear: 0,//选择的年
    nowMonth: 0,//选择的月
    nowMonthStr: '',//转换格式的月
    today: 0
  },

  ready() {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 时分的事件方法 
    selectDateShow() {
      let set_year = this.data.nowYear
      let set_month = this.data.nowMonth
      let set_day = this.data.today
      this.setData({
        date_show: true,
        set_year: set_year,
        set_month: set_month,
        set_day: set_day,

      })
      this.dateInitSet(set_year,set_month,set_day)
    },
    closePopup() {
      if (this.data.today == "00") {
        this.triggerEvent("setPickerDateValue", this.data.todayStr);
        // 年-月-日 格式
        const select_date_year = this.data.nowYear + '-' + this.data.todayStr
        this.triggerEvent("setPickerYearValue", select_date_year);
      }
      this.setData({
        date_show: false
      })
      this.dateInitSet(this.data.set_year,this.data.set_month,this.data.set_day)
      this.triggerEvent("closePopup");

    },

    // 获取当前年月日
    gainDate() {
      let d = Reflect.construct(Date, []);
      this.setData({
        nowYear: d.getFullYear(),
        nowMonth: d.getMonth() + 1,
        today: d.getDate(),
        nowMonthStr: (d.getMonth() + 1).toString().padStart(2, '0'),
        todayStr: (d.getMonth() + 1).toString().padStart(2, '0') + '-' + d.getDate()
      })
    },
    // 上个月
    prev() {
      if (this.data.nowMonth > 1) {
        this.setData({
          nowMonth: this.data.nowMonth - 1,
          nowMonthStr: (this.data.nowMonth - 1).toString().padStart(2, '0')
        })
      } else {
        this.setData({
          nowYear: this.data.nowYear - 1,
          nowMonth: 12,
          nowMonthStr: "12"
        })
      };
      this.dateInitInit()
    },
    // 下个月
    next() {
      if (this.data.nowMonth < 12) {
        this.setData({
          nowMonth: this.data.nowMonth + 1,
          nowMonthStr: (this.data.nowMonth + 1).toString().padStart(2, '0'),
          today: 0
        })
      } else {
        this.setData({
          nowYear: this.data.nowYear + 1,
          nowMonth: 1,
          nowMonthStr: "01",
          today: 0

        })
      }
      this.dateInitInit()
    },
    //今天日期凸显
    todayProminent() {
      // 将今天置为0
      this.setData({
        today: 0
      })
      let d = Reflect.construct(Date, []);
      if (d.getFullYear() == this.data.nowYear && d.getMonth() + 1 == this.data.nowMonth) {
        this.setData({
          today: d.getMonth() + 1 > this.data.nowMonth + 1 ? 0 : d.getDate()
        })
      }
    },
    // 点击选择日期
    clickDate(e) {
      let key = e.currentTarget.dataset.row.text
      let status = e.currentTarget.dataset.row.status
      if (status == "next") {
        this.setData({
          today: key,
          date_show: false,
          nowMonth: this.data.nowMonth + 1,
          nowMonthStr: (this.data.nowMonth + 1).toString().padStart(2, '0')
        })
        this.dateInitClick()
      } else if (status == "pre") {
        this.setData({
          today: key,
          date_show: false,
          nowMonth: this.data.nowMonth - 1,
          nowMonthStr: (this.data.nowMonth - 1).toString().padStart(2, '0')
        })
        this.dateInitClick()
      } else {
        this.setData({
          today: key,
          date_show: false
        })
        this.dateInitClick()
      }



    },
    // 日历主体
    dateInit() {
      let now = Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth - 1]); //当前月份的一号
      let startWeekNow = now.getDay(); //当前月份的一号对应的星期
      let end = Reflect.construct(Date, [Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth]) - 10]);// 当前月份的最后一号
      let endWeekNow = end.getDay(); //当前月份的最后一天对应的星期
      let dayNumNow = Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth, 0]).getDate(); //当前月有多少天
      let dayNumPrev = Reflect.construct(Date, [this.data.nowMonth - 1 ? this.data.nowYear : this.data.nowYear - 1, this.data.nowMonth - 1 ? this.data.nowMonth - 1 : 12, 0]).getDate(); //上个月有多少天

      let prevMonthShow = []; //上个月日期展示的数据
      let nowMonthShow = []; //本月日期展示的数据
      let nextMonthShow = []; //下个月日期展示的数据

      for (let i = 1; i < (startWeekNow ? startWeekNow : 7); i++) {
        prevMonthShow.unshift(
          {
            text: dayNumPrev - i + 1,
            now: false,
            gray: true,
            status: "pre"
          }
        )
      };
      for (let i = 1; i <= dayNumNow; i++) {
        if (this.data.today == i) {
          nowMonthShow.push(
            {
              text: i,
              now: true,
              gray: false

              // 下面代码是将周末置灰以及周末不显示今天的样式
              // now: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? false : true,
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        } else {
          nowMonthShow.push(
            {
              text: i,
              now: false,
              gray: false

              // 下面代码是将周末置灰
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        }
      };
      for (let i = 1; i <= (endWeekNow ? 7 - endWeekNow : 0); i++) {
        nextMonthShow.push(
          {
            text: i,
            now: false,
            gray: true,
            status: "next"
          }
        )
      };
      this.setData({
        month: [...prevMonthShow, ...nowMonthShow, ...nextMonthShow]
      })
      if (this.data.today < 10) {
        this.data.today = '0' + this.data.today
      }

      // 月-日 格式
      const select_date = this.data.nowMonthStr + '-' + this.data.today
      this.triggerEvent("setPickerDateValue", select_date);

      // 年-月-日 格式
      const select_date_year = this.data.nowYear + '-' + this.data.nowMonthStr + '-' + this.data.today
      this.triggerEvent("setPickerYearValue", select_date_year);
    },
    // 日历主体
    dateInitInit() {
      let now = Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth - 1]); //当前月份的一号
      let startWeekNow = now.getDay(); //当前月份的一号对应的星期
      let end = Reflect.construct(Date, [Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth]) - 10]);// 当前月份的最后一号
      let endWeekNow = end.getDay(); //当前月份的最后一天对应的星期
      let dayNumNow = Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth, 0]).getDate(); //当前月有多少天
      let dayNumPrev = Reflect.construct(Date, [this.data.nowMonth - 1 ? this.data.nowYear : this.data.nowYear - 1, this.data.nowMonth - 1 ? this.data.nowMonth - 1 : 12, 0]).getDate(); //上个月有多少天

      let prevMonthShow = []; //上个月日期展示的数据
      let nowMonthShow = []; //本月日期展示的数据
      let nextMonthShow = []; //下个月日期展示的数据

      for (let i = 1; i < (startWeekNow ? startWeekNow : 7); i++) {
        prevMonthShow.unshift(
          {
            text: dayNumPrev - i + 1,
            now: false,
            gray: true,
            status: "pre"
          }
        )
      };
      for (let i = 1; i <= dayNumNow; i++) {
        if (this.data.today == i) {
          nowMonthShow.push(
            {
              text: i,
              now: true,
              gray: false

              // 下面代码是将周末置灰以及周末不显示今天的样式
              // now: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? false : true,
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        } else {
          nowMonthShow.push(
            {
              text: i,
              now: false,
              gray: false

              // 下面代码是将周末置灰
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        }
      };
      for (let i = 1; i <= (endWeekNow ? 7 - endWeekNow : 0); i++) {
        nextMonthShow.push(
          {
            text: i,
            now: false,
            gray: true,
            status: "next"
          }
        )
      };
      this.setData({
        month: [...prevMonthShow, ...nowMonthShow, ...nextMonthShow]
      })
    },
    // 点击选择日期
    dateInitClick() {
      let now = Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth - 1]); //当前月份的一号
      let startWeekNow = now.getDay(); //当前月份的一号对应的星期
      let end = Reflect.construct(Date, [Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth]) - 10]);// 当前月份的最后一号
      let endWeekNow = end.getDay(); //当前月份的最后一天对应的星期
      let dayNumNow = Reflect.construct(Date, [this.data.nowYear, this.data.nowMonth, 0]).getDate(); //当前月有多少天
      let dayNumPrev = Reflect.construct(Date, [this.data.nowMonth - 1 ? this.data.nowYear : this.data.nowYear - 1, this.data.nowMonth - 1 ? this.data.nowMonth - 1 : 12, 0]).getDate(); //上个月有多少天

      let prevMonthShow = []; //上个月日期展示的数据
      let nowMonthShow = []; //本月日期展示的数据
      let nextMonthShow = []; //下个月日期展示的数据

      for (let i = 1; i < (startWeekNow ? startWeekNow : 7); i++) {
        prevMonthShow.unshift(
          {
            text: dayNumPrev - i + 1,
            now: false,
            gray: true,
            status: "pre"
          }
        )
      };
      for (let i = 1; i <= dayNumNow; i++) {
        if (this.data.today == i) {
          nowMonthShow.push(
            {
              text: i,
              now: true,
              gray: false

              // 下面代码是将周末置灰以及周末不显示今天的样式
              // now: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? false : true,
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        } else {
          nowMonthShow.push(
            {
              text: i,
              now: false,
              gray: false

              // 下面代码是将周末置灰
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        }
      };
      for (let i = 1; i <= (endWeekNow ? 7 - endWeekNow : 0); i++) {
        nextMonthShow.push(
          {
            text: i,
            now: false,
            gray: true,
            status: "next"
          }
        )
      };
      this.setData({
        month: [...prevMonthShow, ...nowMonthShow, ...nextMonthShow]
      })
      if (this.data.today < 10) {
        this.data.today = '0' + this.data.today

      }

      // 月-日 格式
      const select_date = this.data.nowMonthStr + '-' + this.data.today
      this.triggerEvent("setPickerDateValueClick", select_date);

      // 年-月-日 格式
      const select_date_year = this.data.nowYear + '-' + this.data.nowMonthStr + '-' + this.data.today
      this.triggerEvent("setPickerYearValueClick", select_date_year);
    },
    // 日历主体
    dateInitSet(set_year, set_month, set_day) {
      this.setData({
        nowYear: set_year,
        nowMonth: set_month,
        today: set_day,
        nowMonthStr: (set_month).toString().padStart(2, '0'),
        todayStr: (set_day).toString().padStart(2, '0')
      })
      let now = Reflect.construct(Date, [set_year, set_month - 1]); //当前月份的一号
      let startWeekNow = now.getDay(); //当前月份的一号对应的星期
      let end = Reflect.construct(Date, [Reflect.construct(Date, [set_year, set_month]) - 10]);// 当前月份的最后一号
      let endWeekNow = end.getDay(); //当前月份的最后一天对应的星期
      let dayNumNow = Reflect.construct(Date, [set_year, set_month, 0]).getDate(); //当前月有多少天
      let dayNumPrev = Reflect.construct(Date, [set_month - 1 ? set_year : set_year - 1, set_month - 1 ? set_month - 1 : 12, 0]).getDate(); //上个月有多少天

      let prevMonthShow = []; //上个月日期展示的数据
      let nowMonthShow = []; //本月日期展示的数据
      let nextMonthShow = []; //下个月日期展示的数据

      for (let i = 1; i < (startWeekNow ? startWeekNow : 7); i++) {
        prevMonthShow.unshift(
          {
            text: dayNumPrev - i + 1,
            now: false,
            gray: true,
            status: "pre"
          }
        )
      };
      for (let i = 1; i <= dayNumNow; i++) {
        if (set_day == i) {
          nowMonthShow.push(
            {
              text: i,
              now: true,
              gray: false

              // 下面代码是将周末置灰以及周末不显示今天的样式
              // now: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? false : true,
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        } else {
          nowMonthShow.push(
            {
              text: i,
              now: false,
              gray: false

              // 下面代码是将周末置灰
              // gray: ((startWeekNow + i - 1) % 7 == 6 || (startWeekNow + i - 1) % 7 == 0) ? true : false
            }
          )
        }
      };
      for (let i = 1; i <= (endWeekNow ? 7 - endWeekNow : 0); i++) {
        nextMonthShow.push(
          {
            text: i,
            now: false,
            gray: true,
            status: "next"
          }
        )
      };
      this.setData({
        month: [...prevMonthShow, ...nowMonthShow, ...nextMonthShow]
      })

    },
  }
})
